package com.teacher.service.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.teacher.pojo.entity.Behavior;
import com.teacher.pojo.entity.PerStudent;
import com.teacher.pojo.excel.BehaviorExcel;
import com.teacher.service.IBehaviorService;
import com.teacher.service.IPerStudentService;
import com.teacher.service.ISuccessMessage;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: zfm
 * @Date: 2021/7/30 16:17
 */
public class ExcelBehaviorListener extends AnalysisEventListener<BehaviorExcel> {
    private Long termId;
    private IBehaviorService iBehaviorService;

    private List<Behavior> behaviorList = new ArrayList<>();
    private IPerStudentService iPerStudentService;

    public ExcelBehaviorListener(Long termId, IBehaviorService iBehaviorService, IPerStudentService iPerStudentService) {
        this.termId = termId;
        this.iBehaviorService = iBehaviorService;
        this.iPerStudentService = iPerStudentService;
    }

    @Override
    public void invoke(BehaviorExcel behaviorExcel, AnalysisContext analysisContext) {
        if (iPerStudentService.isExit(behaviorExcel.getStudentKey())) {
            Behavior behavior = new Behavior();
            BeanUtils.copyProperties(behaviorExcel, behavior);
            behavior.setTermId(termId);
            behaviorList.add(behavior);
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        saveData();
    }

    private void saveData() {
        iBehaviorService.saveBatch(behaviorList);
    }
}
